#include "MainMenuScreen.h"
#include "SimpleAudioEngine.h"
#include "Supporting/Enums/EnumScreenType.h"
#include "Supporting/Enums/EnumTutorialType.h"
#include "Supporting/TutorialLoader.h"
#include "Supporting/SoundLoader.h"
#include "../FlowController.h"

using namespace cocos2d;
using namespace CocosDenshion;

//===== CONSTRUCTOR =====
MainMenuScreen::MainMenuScreen() : BaseScreen()
{
	_screenID = SCREEN_MAIN;
	_bgFileName = CCString::create("Menu/[UI] Main.png");
}
//===== DESTRUCTOR =======
MainMenuScreen::~MainMenuScreen()
{
}

/*
 * @function: createLayer
 * @brief: Create MainMenuScreen layer
 * @return: MainMenuScreen layer
 */
BaseScreen* MainMenuScreen::createLayer()
{
    // 'layer' is an autorelease object
	MainMenuScreen *layer = MainMenuScreen::create();

    // return the scene
    return layer;
}

/*
 * @function: loadScreen
 * @brief: Load the screen for main menu screen
 * @param flowcontroller: reference to FlowController
 * @param bgfilename: filename to the background image
 * @result: create the main menu screen
 */
void MainMenuScreen::loadScreen(FlowController *flowcontroller)
{
	BaseScreen::loadScreen(flowcontroller);

	CCSize _screenSize = CCDirector::sharedDirector()->getWinSize();

	//================BUTTONS======================
	//Start button
	//isPaused = false;
	CCSprite * playButtonSprite = CCSprite::create("Menu/[UI] Main Play (Normal).png");
	CCSprite * playButtonSpritePressed = CCSprite::create("Menu/[UI] Main Play (Pressed).png");
	CCMenuItemSprite* playButton = CCMenuItemSprite::create(playButtonSprite,playButtonSpritePressed,this,menu_selector(MainMenuScreen::playButtonPressed));
	playButton->setScale(_scaleFactor);
	playButton->setPosition(ccp(_screenSize.width - (190*_scaleFactor),(100*_scaleFactor)));

	//SFX button
	CCSprite * soundButtonSprite;
	CCSprite * soundButtonSpritePressed;
	if( !SoundLoader::getIsSoundEffect() )
	{
		soundButtonSprite = CCSprite::create("Game/Pause/[Button] SFX - Normal.png");
		soundButtonSpritePressed = CCSprite::create("Game/Pause/[Button] SFX - Pressed.png");
	}
	else
	{
		soundButtonSprite = CCSprite::create("Game/Pause/[Button] SFX - Mute.png");
		soundButtonSpritePressed = CCSprite::create("Game/Pause/[Button] SFX - Mute Pressed.png");
	}
	CCMenuItemSprite* soundButton = CCMenuItemSprite::create(soundButtonSprite,soundButtonSpritePressed,this,menu_selector(MainMenuScreen::soundButtonPressed));
	soundButton->setScale(_scaleFactor);
	soundButton->setTag(102);
	soundButton->setPosition(ccp(_screenSize.width - (70*_scaleFactor),_screenSize.height - (70*_scaleFactor)));

	//Music button
	CCSprite * musicButtonSprite;
	CCSprite * musicButtonSpritePressed;
	if( !SoundLoader::getIsBackgroundMusic() )
	{
		musicButtonSprite = CCSprite::create("Game/Pause/[Button] Music - Normal.png");
		musicButtonSpritePressed = CCSprite::create("Game/Pause/[Button] Music - Pressed.png");
	}
	else
	{
		musicButtonSprite = CCSprite::create("Game/Pause/[Button] Music - Mute.png");
		musicButtonSpritePressed = CCSprite::create("Game/Pause/[Button] Music - Mute Pressed.png");
	}
	CCMenuItemSprite* musicButton = CCMenuItemSprite::create(musicButtonSprite,musicButtonSpritePressed,this,menu_selector(MainMenuScreen::musicButtonPressed));
	musicButton->setScale(_scaleFactor);
	musicButton->setTag(101);
	musicButton->setPosition(ccp(_screenSize.width - (190*_scaleFactor),_screenSize.height - (70*_scaleFactor)));

    CCMenu* mainmenu = CCMenu::create(playButton, soundButton, musicButton, NULL);
    mainmenu->setPosition(0,0);
    mainmenu->setTag(100);
	this->addChild(mainmenu);
}

/*!
 * @function: playButtonPressed
 * @brief: Switch to skill select screen when start button is pressed
 * @result: Switch to skill select screeen
 */
void MainMenuScreen::playButtonPressed()
{
	//Switch screen to level select screen
	_myFlowController->switchScreenTo(SCREEN_LEVELSELECT);
}

/*!
 * @function: musicButtonPressed
 * @brief: Toggle music button
 * @result: Enable/disable background music
 */
void MainMenuScreen::musicButtonPressed()
{
	CCMenu* menu = (CCMenu*)this->getChildByTag(100);
	CCMenuItemSprite *sfxButton = (CCMenuItemSprite*)menu->getChildByTag(101);
	if( !SoundLoader::getIsBackgroundMusic() )
	{
		sfxButton->setNormalImage(CCSprite::create("Game/Pause/[Button] Music - Mute.png"));
		sfxButton->setSelectedImage(CCSprite::create("Game/Pause/[Button] Music - Mute Pressed.png"));
	}
	else
	{
		sfxButton->setNormalImage(CCSprite::create("Game/Pause/[Button] Music - Normal.png"));
		sfxButton->setSelectedImage(CCSprite::create("Game/Pause/[Button] Music - Pressed.png"));
	}

	SoundLoader::toggleBackgroundMusic();
}

/*!
 * @function: soundButtonPressed
 * @brief: Toggle sound button
 * @result: Enable/disable SFX
 */
void MainMenuScreen::soundButtonPressed()
{
	CCMenu* menu = (CCMenu*)this->getChildByTag(100);
	CCMenuItemSprite *sfxButton = (CCMenuItemSprite*)menu->getChildByTag(102);
	if( !SoundLoader::getIsSoundEffect() )
	{
		sfxButton->setNormalImage(CCSprite::create("Game/Pause/[Button] SFX - Mute.png"));
		sfxButton->setSelectedImage(CCSprite::create("Game/Pause/[Button] SFX - Mute Pressed.png"));
	}
	else
	{
		sfxButton->setNormalImage(CCSprite::create("Game/Pause/[Button] SFX - Normal.png"));
		sfxButton->setSelectedImage(CCSprite::create("Game/Pause/[Button] SFX - Pressed.png"));
	}

	SoundLoader::toggleSoundEffect();
}

/*
 * @function: keyBackClicked
 * @brief: hardware back button function
 */
void MainMenuScreen::keyBackClicked()
{
	CCDirector::sharedDirector()->end();

	#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
		exit(0);
	#endif
}
